package com.liuwei.msmall.product.controller.api;

import com.google.common.collect.Lists;
import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.R;
import com.liuwei.msmall.product.entity.AttrAttrgroupRelationEntity;
import com.liuwei.msmall.product.entity.AttrGroupEntity;
import com.liuwei.msmall.product.service.AttrAttrgroupRelationService;
import com.liuwei.msmall.product.service.AttrGroupService;
import com.liuwei.msmall.product.vo.AttrVo;
import com.liuwei.msmall.product.vo.req.AttrAttrgroupRelationReq;
import com.liuwei.msmall.product.vo.resp.AttrGroupWithAttrRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author llww
 * @email 2456888577@qq.com
 * @date 2021-02-25 17:45:01
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {


    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private AttrAttrgroupRelationService relationService;

    /**
     * 查出分类下关联的属性分组以及分组下的所有属性
     */
    @GetMapping("/{cateId}/withattr")
    public R getAttrGroupAndAttrByCateId(@PathVariable("cateId") Long cateId){

       List<AttrGroupWithAttrRespVo> vos =  attrGroupService.getAttrGroupAndAttrByCateId(cateId);

        return R.ok().put("data",vos);
    }




    /**
     * 删除分组与属性的关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrAttrgroupRelationReq> relationVos){
        relationService.removeRelationBatch(relationVos);
        return R.ok();
    }



    /**
     * 保存分组与属性的关联关系
     */
    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrAttrgroupRelationReq> relationVos){

        if (CollectionUtils.isEmpty(relationVos)){
            return R.ok();
        }
        List<AttrAttrgroupRelationEntity> entities = Lists.newArrayList();
        relationVos.stream().forEach(item->{
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item,attrAttrgroupRelationEntity);
            entities.add(attrAttrgroupRelationEntity);
        });

        relationService.saveBatch(entities);

        return R.ok();
    }



    /**
     * 查询分组关联或非关联的属性信息
     */
    @RequestMapping("/{attrgroupId}/{type}/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long  attrgroupId
            ,@PathVariable("type") String type,@RequestParam Map<String, Object> params){

        if ("attr".equals(type)){
            List<AttrVo> attrVos= attrGroupService.getAttrRelationList(attrgroupId);
            return R.ok().setData(attrVos);
        }else if ("noattr".equals(type)){
            PageUtils page = attrGroupService.getNoAttrRelationList(attrgroupId,params);
            return R.ok().put("page",page);
        }
       return R.error("路径不匹配");
    }



    /**
     * 根据分类id查询属性分组信息
     */
    @GetMapping("/list/{categoryId}")
    public R categoryList(@PathVariable("categoryId") Long categoryId,@RequestParam Map<String,Object> params){

        PageUtils page = attrGroupService.queryPage(categoryId,params);
        return R.ok().put("page",page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        //获取带分类路径的属性分组信息
        AttrGroupEntity attrGroup  = attrGroupService.getOneWithCatePath(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
